package com.ywu.configcenter.config;

import com.ywu.configcenter.entity.ConfigCenterEntity;
import com.ywu.configcenter.jdbc.JdbcUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.env.EnvironmentPostProcessor;
import org.springframework.boot.env.OriginTrackedMapPropertySource;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.MutablePropertySources;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @ClassName DefineEnvironmentPostProcessor
 * @Description 读取数据库配置
 * @Author GroundDemo
 * @Date 2024/2/27 22:06
 * @Version 1.0
 **/
@Component
@Slf4j
public class DefineEnvironmentPostProcessor implements EnvironmentPostProcessor {
    private static String env = null;

    private static String url;

    private static String user;

    private static String password;

    private static String driver;

    @Override
    public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
        // 获取配置参数
        if (StringUtils.isAnyBlank(url, user, password, driver)) {
            url = environment.getProperty("ywu.config.datasource.url");
            user = environment.getProperty("ywu.config.datasource.user");
            password = environment.getProperty("ywu.config.datasource.password");
            driver = environment.getProperty("ywu.config.datasource.driver.class");
        }
        Connection connection;
        try {
            connection = JdbcUtils.getConnection(driver, url, user, password);
        } catch (ClassNotFoundException | SQLException e) {
            log.error("DefineEnvironmentPostProcessor get connection error {}", e.getMessage());
            throw new RuntimeException(e.getMessage());
        }
        String applicationName = environment.getProperty("spring.application.name");
        if (Objects.isNull(env)) {
            env = environment.getProperty("spring.profiles.active");
        }
        List<ConfigCenterEntity> configCenterEntities = new ArrayList<>();
        List<ConfigCenterEntity> applicationEntities = JdbcUtils.getConfigByProfileAndApplication(connection, applicationName, env);
        if (!CollectionUtils.isEmpty(applicationEntities)) {
            configCenterEntities.addAll(applicationEntities);
        }
        List<ConfigCenterEntity> globalEntities = JdbcUtils.getConfigByProfileAndApplication(connection, "GLOBAL", env);
        if (!CollectionUtils.isEmpty(globalEntities)) {
            configCenterEntities.addAll(globalEntities);
        }
        MutablePropertySources propertySources = environment.getPropertySources();
        Map<String, String> properties = null;
        if (!CollectionUtils.isEmpty(configCenterEntities)) {
            properties = configCenterEntities.stream().collect(Collectors.toMap(ConfigCenterEntity::getKey,
                    ConfigCenterEntity::getValue, (value1, value2) -> value1));
        }
        if (Objects.isNull(properties)) {
            properties = new HashMap<>();
        }
        String name = "ywuMysqlConfig";
        OriginTrackedMapPropertySource ps = new OriginTrackedMapPropertySource(name, properties, true);
        propertySources.remove(name);
        propertySources.addFirst(ps);
    }
}
